/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package de.titama.control;


import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;

import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ListView;
import javafx.stage.Stage;


/**
 *
 * @author Thomas Müller
 * 
 */

public class CategoryController implements Initializable {
    
    DBConnect dbcon = new DBConnect();
    
    @FXML ListView CategoryList;
    
    ObservableList<String> data = FXCollections.observableArrayList ();
    
    
    
    @Override
    public void initialize(URL url, ResourceBundle rb)                    
    {
        refreshList();

    }
    
    /**
     * 
     * Ruft eine Eingabemaske zum erstellen einer neuen Kategorie auf
     * 
     * @param event Ereignis
     */
    
    public void AddCategory(ActionEvent event) {
        Parent root;
        
        try 
        {
            root = FXMLLoader.load(getClass().getResource("/de/titama/view/AddCategory.fxml"));
            Stage stage = new Stage();
            
            stage.setTitle("Kategorie hinzugügen ...");
            stage.setScene(new Scene(root, 300, 75));
            stage.setResizable(false);
            stage.show();
            
        } 
        catch (IOException e) 
        {
            
        }
    }
    
    
    /**
     * 
     * Löscht die ausgewählte Kategorie aus der Datenbank
     * 
     * @param e 
     */
    
    @FXML
    public void Delete(ActionEvent e)
    {
        dbcon.deleteCategory(CategoryList.getSelectionModel().getSelectedItem().toString());
        refreshList();
    }
    
    /**
     * 
     * Schließt das Fenster
     * 
     * @param e 
     */
    
    @FXML
    public void Close(ActionEvent e)
    {
        Parent root;
        try{
                Node  source = (Node)  e.getSource(); 
                Stage stage  = (Stage) source.getScene().getWindow();
                stage.close();  
        }
        catch(Exception exe)
        {
        
        }
    } 
    
    /**
     * 
     * Aktualisiert die Liste mit den Kategorien 
     * 
     */   
    
    
    public void refreshList()
    {
        data.clear();        
        
        for(String s : dbcon.getCategorys())
        {
            data.add(s);
        }
        
        CategoryList.setItems(data);      
    }
       
    

    
    
}
